/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.manager.search;

import br.com.foxline.enums.StatusSale;
import br.com.foxline.model.*;
import br.com.foxline.service.LotService;
import br.com.foxline.service.PendingService;
import br.com.foxline.service.SaleService;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;

/**
 *
 * @author luciano
 */
@ManagedBean
@ViewScoped
public class ManagerSearchSale implements Serializable {

    @EJB
    private SaleService saleService;
    @EJB
    private PendingService pendingService;
    @EJB
    private LotService lotService;
    private Sale sale;
    private Enterprise enterprise;
    private Integer block;
    private Integer lot;
    private Date dateSale;
    private Customer customer;
    private List<Sale> sales;
    
    @PostConstruct
    public void init() {
        this.sale = new Sale();
        this.customer = new Customer();
        this.enterprise = new Enterprise();
        this.sales = new ArrayList<Sale>();
    }
    
    public boolean canModify(Sale s){
        return saleService.verifyIfCanApprove(s);
    }

    public List<SelectItem> selectTypeBlock() {
        List<SelectItem> items = new ArrayList<SelectItem>();
        if (enterprise != null && enterprise.getQtdBlock() != null) {
            for (int i = 1; i <= enterprise.getQtdBlock(); i++) {
                items.add(new SelectItem(i, "Quadra " + i));
            }
        }
        return items;
    }

    public List<SelectItem> lotsByEnterpriseAndBlock() {
        List<SelectItem> items = new ArrayList<SelectItem>();
        List<Lot> lots = lotService.lotsByEnterprise(enterprise, block);
        for (Lot lot : lots) {
            items.add(new SelectItem(lot.getId(), "Lote " + lot.getNum().toString()));
        }
        return items;
    }

    public List<Pending> pendingsBySale(Sale s) {
        return pendingService.pendingBySale(s);
    }

    public void search() {
        sales = saleService.findSale(enterprise, block, lot, customer, sale.getStatusSale(), dateSale);
    }

    public List<SelectItem> pendingType() {
        List<SelectItem> items = new ArrayList<SelectItem>();
        for (StatusSale c : StatusSale.values()) {
            items.add(new SelectItem(c, c.getName()));
        }
        return items;
    }

    public Sale getSale() {
        return sale;
    }

    public void setSale(Sale sale) {
        this.sale = sale;
    }

    public List<Sale> getSales() {
        return sales;
    }

    public void setSales(List<Sale> sales) {
        this.sales = sales;
    }

    public Enterprise getEnterprise() {
        return enterprise;
    }

    public void setEnterprise(Enterprise enterprise) {
        this.enterprise = enterprise;
    }

    public Integer getBlock() {
        return block;
    }

    public void setBlock(Integer block) {
        this.block = block;
    }

    public Date getDateSale() {
        return dateSale;
    }

    public void setDateSale(Date dateSale) {
        this.dateSale = dateSale;
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public Integer getLot() {
        return lot;
    }

    public void setLot(Integer lot) {
        this.lot = lot;
    }

    public void reportContract(Long l){
        sale = saleService.find(l);
//        saleService.reportsContract(sale);
        saleService.reportsProposedContract(sale);
    }
    
    public boolean canEdit(Sale s){
        if(s.getStatusSale().equals(StatusSale.FINALIZED)){
            return false;
        }
        return true;
    }
  
    public boolean canGenerateContract(Sale s){
        if(s.getStatusSale().equals(StatusSale.FINALIZED)){
            return true;
        }
        return false;    
    }
    
    
}
